//
//  Fengly_nuo_SQLDataBaseHandle.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 Fengly_Nuo. All rights reserved.
//

#import "Fengly_nuo_SQLDataBaseHandle.h"
#import "ActivityModel.h"
#import "MovieModel.h"

#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kDbPath(sqlListName) [document stringByAppendingPathComponent:sqlListName]

@implementation Fengly_nuo_SQLDataBaseHandle

+ (Fengly_nuo_SQLDataBaseHandle *)sharedDataBaseHandle {
    // 静态修饰指针
    static Fengly_nuo_SQLDataBaseHandle *handle = nil;
    // 线程只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[Fengly_nuo_SQLDataBaseHandle alloc] init];
    });
    return handle;
}
// 数据库
- (void)openDataBase {
    // 获取沙盒路径
    NSString *document = kDocumentPath;
    // 拼接数据库路径
    NSString *dbPath = kDbPath(@"douBan.sqlite");
    NSLog(@"%@", dbPath);
    
    // 打开数据库
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
}

- (void)createTableWithTableName:(NSString *)string {
    if ([string isEqualToString:@"activityList"]) {
        // sql语句
        NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists %@(number integer primary key autoincrement, title text, image text, content text, begin_time text, end_time text, wisher_count text, participant_count text, address text, category_name text, ownerName text)", string];
        // 让数据库执行sql语句（数据库必须是打开状态）
        NSString *str = [NSString stringWithFormat:@"创建%@表", string];
        [self executeSQLString:sqlStr showInfo:str];
    } else if ([string isEqualToString:@"movieList"]) {
        // sql语句
        NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists %@(number integer primary key autoincrement, title text)", string];
        // 让数据库执行sql语句（数据库必须是打开状态）
        NSString *str = [NSString stringWithFormat:@"创建%@表", string];
        [self executeSQLString:sqlStr showInfo:str];
    }
}



#pragma mark - 活动收藏表操作实现
- (void)insertActivityModel:(ActivityModel *)model {
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into activityList(title, image, content, begin_time, end_time, wisher_count, participant_count, address, category_name, ownerName) values('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", model.title, model.image, model.content, model.begin_time, model.end_time, model.wisher_count, model.participant_count, model.address, model.category_name, model.ownerName];
    // 数据库执行
    [self executeSQLString:sqlStr showInfo:@"添加活动"];
}

- (void)deleteActivityModel:(NSString *)string {
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"delete from activityList where title = '%@'", string];
    // 数据库执行
    [self executeSQLString:sqlStr showInfo:@"删除活动"];
}

- (NSArray *)selectTable {
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from activityList"];
    // 创建一个跟随指针，用来遍历数据库中的每一条数据
    sqlite3_stmt *stmt = nil;
    // 执行sql语句
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 创建数组
    NSMutableArray *arrTitles = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 从数据库中取出数据
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            // 创建model
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [arrTitles addObject:model.title];
        }
    } else {
        NSLog(@"查询失败");
    }
    return arrTitles;
}

- (ActivityModel *)selectActivity:(NSString *)activityTitle {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from activityList where title = '%@'", activityTitle];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 查询成功
    if (result == SQLITE_OK) {
        // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *image = sqlite3_column_text(stmt, 2);
            const unsigned char *content = sqlite3_column_text(stmt, 3);
            const unsigned char *begin_time = sqlite3_column_text(stmt, 4);
            const unsigned char *end_time = sqlite3_column_text(stmt, 5);
            const unsigned char *wisher_count = sqlite3_column_text(stmt, 6);
            const unsigned char *participant_count = sqlite3_column_text(stmt, 7);
            const unsigned char *address = sqlite3_column_text(stmt, 8);
            const unsigned char *category_name = sqlite3_column_text(stmt, 9);
            const unsigned char *ownerName = sqlite3_column_text(stmt, 10);
            
            // 创建model
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            model.image = [NSString stringWithUTF8String:(const char *)image];
            model.content = [NSString stringWithUTF8String:(const char *)content];
            model.begin_time = [NSString stringWithUTF8String:(const char *)begin_time];
            model.end_time = [NSString stringWithUTF8String:(const char *)end_time];
            model.wisher_count = [NSString stringWithUTF8String:(const char *)wisher_count];
            model.participant_count = [NSString stringWithUTF8String:(const char *)participant_count];
            model.address = [NSString stringWithUTF8String:(const char *)address];
            model.category_name = [NSString stringWithUTF8String:(const char *)category_name];
            model.ownerName = [NSString stringWithUTF8String:(const char *)ownerName];
            
            return model;
        }
    }
    return nil;
}



#pragma mark - 电影收藏表操作实现
- (void)insertMovieModel:(MovieModel *)model {
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into movieList(title) values('%@')", model.title];
    // 执行sql语句
    [self executeSQLString:sqlStr showInfo:@"收藏电影"];
}

- (void)deleteMovieModel:(NSString *)string {
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"delete from movieList where title = '%@'", string];
    // 执行sql语句
    [self executeSQLString:sqlStr showInfo:@"删除电影"];
}

- (NSArray *)selectMovieTable {
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from movieList"];
    // 创建跟随指针
    sqlite3_stmt *stmt = nil;
    // 执行sql语句
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 创建数组
    NSMutableArray *arrTitle = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 从数据库中取出数据
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            MovieModel *model = [MovieModel movieModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [arrTitle addObject:model.title];
        }
    } else {
        NSLog(@"查询失败");
    }
    return arrTitle;
}



- (void)deleteAll {
    NSString *sqlStr1 = [NSString stringWithFormat:@"delete from activityList"];
//    int result1 = sqlite3_exec(dbPoint, sqlStr1.UTF8String, NULL, NULL, NULL);
    NSString *sqlStr2 = [NSString stringWithFormat:@"delete from movieList"];
//    int result2 = sqlite3_exec(dbPoint, sqlStr2.UTF8String, NULL, NULL, NULL);
    [self executeSQLString:sqlStr1 showInfo:nil];
    [self executeSQLString:sqlStr2 showInfo:nil];
}



/** 判断数据库操作是否成功 */
- (void)executeSQLString:(NSString *)sqlString showInfo:(NSString *)info {
    NSString *str = @"";
    int result = sqlite3_exec(dbPoint, sqlString.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        str = [NSString stringWithFormat:@"%@ 成功", info];
    } else {
        str = [NSString stringWithFormat:@"%@ 失败", info];
    }
    NSLog(@"%@", str);
}


@end
